﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace Splittergame
{
    class Ballsimulation
    {

        public List<Ball> BallList;
        private const int MAX_BALLS = 1;
        public MouseState m_mouseState;
        public Vector2 m_mousePosition;


        public Ballsimulation() 
        {
        BallList = new List<Ball>();

        }

        internal void LoadContent() 
        {

            for (int i = 0; i < MAX_BALLS; i++)
            {
                BallList.Add(new Ball(i));
            }    
                
        }

        internal void Update(GameTime gameTime, MouseState mouseState, Vector2 LogicalMousePos)
        {
            m_mouseState = mouseState;
            m_mousePosition = LogicalMousePos;
            float m_elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;

            foreach (Ball ball in BallList)
            {
                if (ball != null)
                {

                    Vector2 position = ball.getBallPosition();

                    position.X += m_elapsedTime * ball.ballSpeed.X;
                    position.Y += m_elapsedTime * ball.ballSpeed.Y;

                    ball.setBallPosition(position.X, position.Y);
                }
            }
            BallCollision();
        }

        internal void BallHit(Vector2 CrossHairPosition)
        {

            Vector2 MousePosition = new Vector2(m_mouseState.X, m_mouseState.Y);
            foreach (Ball ball in BallList)
	        {
                //if (ball.ballSpeed != new Vector2(0,0))
                //{

                    if (Vector2.Distance(m_mousePosition, ball.getBallPosition()) < 1.0f)
                    {
                        //ball.ballRadius = 0;
                        ball.ballSpeed = new Vector2(0, 0);
                        //System.Diagnostics.Debug.WriteLine("träff");
                        Console.WriteLine(Vector2.Distance(m_mousePosition, ball.getBallPosition()));
                    }
                    else
                    {
                        //System.Diagnostics.Debug.WriteLine(m_mousePosition);
                        //Console.WriteLine(ball.getBallPosition());
                        Console.WriteLine(Vector2.Distance(m_mousePosition, ball.getBallPosition()));
                    }
                //}
            }
        }
        internal void BallCollision()
        {

            foreach (Ball ball in BallList)
            {
                if (ball != null)
                {

                    Vector2 position = ball.getBallPosition();

                    if (position.X + ball.ballRadius > 10)
                    {
                        ball.ballSpeed.X = ball.ballSpeed.X * -1;

                    }
                    if (position.Y + ball.ballRadius > 10)
                    {
                        ball.ballSpeed.Y = ball.ballSpeed.Y * -1;

                    }
                    if (position.X < 0)
                    {
                        ball.ballSpeed.X = ball.ballSpeed.X * -1;

                    }
                    if (position.Y < 0)
                    {
                        ball.ballSpeed.Y = ball.ballSpeed.Y * -1;

                    }
                }
            }
        }
    }
}
